package com.wolfalpha.utron.service.impl;

import android.text.TextUtils;

import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.ItemProject;
import com.wolfalpha.utron.bean.ItemProjectComment;
import com.wolfalpha.utron.bean.Project;
import com.wolfalpha.utron.bean.UserBaseInfo;
import com.wolfalpha.utron.net.URLs;
import com.wolfalpha.utron.service.ProjectService;
import com.wolfalpha.utron.utils.JsonUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luodon on 15/8/20.
 */
public class ProjectServiceImpl implements ProjectService {


    @Override
    public List<ItemProject> getList(int page, int order, List<Integer> dirs) {
        List<ItemProject> projects = new ArrayList<ItemProject>();
        Map<String, String> mp = new HashMap<>();
        mp.put("page", String.valueOf(page));
        if (order == 0) {
            mp.put("order", "time");
        }
        if (order == 1) {
            mp.put("order", "hot");
        }
        if (order == 2) {
            mp.put("order", "dir");
            for (int i = 0; i < dirs.size(); i++)
                mp.put("dirs[" + String.valueOf(i) + "]", String.valueOf(dirs.get(i)));
        }
        if (order == 3) {
            mp.put("order", "rand");
        }
        String result = AppContext.getApiClient().sendPostResquest(URLs.PROJECT_URL + "getList", mp);

        if (result == null) {
            return null;
        }
        if (TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        System.out.println(result);
        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                ItemProject project = JsonUtils.paraseJson(arr.get(i).toString(), ItemProject.class);
                projects.add(project);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
        return projects;
    }

    @Override
    public List<ItemProject> getMyList(int userId, int page) {
        List<ItemProject> projects = new ArrayList<ItemProject>();
        Map<String, String> mp = new HashMap<>();
        mp.put("page", String.valueOf(page));
        mp.put("user_id", String.valueOf(userId));
        String result = AppContext.getApiClient().sendPostResquest(URLs.PROJECT_URL + "getMyList", mp);
        if (result == null || TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        System.out.println(result);
        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                ItemProject project = JsonUtils.paraseJson(arr.get(i).toString(), ItemProject.class);
                projects.add(project);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
        return projects;
    }

    @Override
    public Project getDetail(int pId) {
        Map<String, String> mp = new HashMap<>();
        mp.put("project_id", String.valueOf(pId));
        String result = AppContext.getApiClient().sendPostResquest(URLs.PROJECT_URL + "getDetail", mp);
        if (result == null) {
            return null;
        }
        System.out.println(result);
        return JsonUtils.paraseJson(JsonUtils.getData(result), Project.class);
    }

    @Override
    public boolean praise(int pId) {
        Map<String, String> mp = new HashMap<>();
        mp.put("project_id", String.valueOf(pId));
        String result = AppContext.getApiClient().sendPostResquest(URLs.PROJECT_URL + "good", mp);
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean doComment(int pId, String content, int toId) {
        Map<String, String> mp = new HashMap<>();
        mp.put("project_id", String.valueOf(pId));
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("to_id", String.valueOf(toId));
        mp.put("content", content);
        String result = AppContext.getApiClient().sendPostResquest(URLs.PROJECT_URL + "comment", mp);
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean doWillJoin(int pId) {
        Map<String, String> mp = new HashMap<>();
        mp.put("project_id", String.valueOf(pId));
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        String result = AppContext.getApiClient().sendPostResquest(URLs.PROJECT_URL + "will", mp);
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean delete(int pId) {
        Map<String, String> mp = new HashMap<>();
        mp.put("project_id", String.valueOf(pId));
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("password", String.valueOf(AppContext.getInstance().getProperty("utron_pwd")));
        String result = AppContext.getApiClient().sendPostResquest(URLs.PROJECT_URL + "delete", mp);
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean publish(String name, int dir, String location, String progress, String teamer, String intro, String detail, String logo, String calls, int owner) {
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("name", name);
        mp.put("location", location);
        if (owner != 0 && owner != 1) return false;
        mp.put("owner", String.valueOf(owner));
        mp.put("progress", progress);
        mp.put("teamer", teamer);
        mp.put("intro", intro);
        mp.put("detail", detail);
        mp.put("logo", logo);
        mp.put("dir", String.valueOf(dir));
        mp.put("calls", calls);
        String result = AppContext.getApiClient().sendPostResquest(URLs.PROJECT_URL + "publish", mp);
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public List<UserBaseInfo> getAttentions(int pId) {
        List<UserBaseInfo> baseInfos = new ArrayList<UserBaseInfo>();
        Map<String, String> mp = new HashMap<>();
        mp.put("project_id", String.valueOf(pId));
        String result = AppContext.getApiClient().sendPostResquest(URLs.PROJECT_URL + "getAttention", mp);
        if (result == null || JsonUtils.getData(result) == null) {
            return null;
        }
        System.out.println(result);
        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                UserBaseInfo userBaseInfo = JsonUtils.paraseJson(arr.get(i).toString(), UserBaseInfo.class);
                baseInfos.add(userBaseInfo);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }

        return baseInfos;
    }

    @Override
    public boolean doAttention(int pId, boolean status) {
        Map<String, String> mp = new HashMap<>();
        mp.put("project_id", String.valueOf(pId));
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("do", status ? "add" : "cancel");
        String result = AppContext.getApiClient().sendPostResquest(URLs.PROJECT_URL + "attention", mp);
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean checkAttention(int pId) {

        Map<String, String> mp = new HashMap<>();
        mp.put("project_id", String.valueOf(pId));
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        String result = AppContext.getApiClient().sendPostResquest(URLs.PROJECT_URL + "checkAttention", mp);
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getData(result).equals("1");
    }

    @Override
    public List<ItemProjectComment> getComments(int pId, int page, int fId) {
        List<ItemProjectComment> comments = new ArrayList<ItemProjectComment>();
        Map<String, String> mp = new HashMap<>();
        mp.put("project_id", String.valueOf(pId));
        mp.put("page", String.valueOf(page));
        mp.put("to_id", String.valueOf(fId));
        String result = AppContext.getApiClient().sendPostResquest(URLs.PROJECT_URL + "getComments", mp);
        if (TextUtils.isEmpty(result)) {
            return null;
        }
        if (TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        System.out.println(result);
        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                ItemProjectComment comment = JsonUtils.paraseJson(arr.get(i).toString(), ItemProjectComment.class);
                comments.add(comment);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
        return comments;
    }
}
